package com.scut.dispatching_server.service;

import com.scut.dispatching_server.config.GlobalVariables;
import com.scut.dispatching_server.model.Relationship;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Component;
import org.springframework.web.client.RestTemplate;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.logging.Logger;

@Component
public class RelationshipPost {
    @Autowired
    private RestTemplate restTemplate;
    private static final Logger sysLogger = Logger.getLogger("sysLog");
    public void initRelationship(HashMap<String, ArrayList<String>> manyToManyRelationship) {
        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.APPLICATION_JSON);
        String url = GlobalVariables.DEMO_ADDRESS + GlobalVariables.DEMO_RE_ADD_PATH;
        HttpEntity<HashMap<String, ArrayList<String>>> entity = new HttpEntity<>(manyToManyRelationship, headers);
        restTemplate.postForEntity(url, entity, String.class);
    }

    public void selectRelationship(Relationship relationship) {
        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.APPLICATION_JSON);
        String url = GlobalVariables.DEMO_ADDRESS + GlobalVariables.DEMO_RE_UPDATE_PATH;
        HttpEntity<Relationship> entity = new HttpEntity<>(relationship, headers);
        sysLogger.info("------------------------selected--------------------");
        restTemplate.postForEntity(url, entity, String.class);
    }

    public void clearRelationship() {
        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.APPLICATION_JSON);
        String url = GlobalVariables.DEMO_ADDRESS + GlobalVariables.DEMO_RE_DELETE_PATH;
        restTemplate.postForEntity(url, null, String.class);
    }
}
